﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Configuration;

namespace DentistOffice
{
    public partial class Pacjent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var DE = new DentistEntities())
            {
                int id = 0;
                try
                {
                    id = Int32.Parse(Request.QueryString["p"]);
                }
                catch (Exception)
                {
                    wiadomosc.Visible = true;
                    calosc.Visible = false;
                    infop.Text = "Nie podano id pacjenta!";
                    return;
                }
                int i = 0;
                Session.Contents.RemoveAll();
                var pchk = from p in DE.Patients where p.id == id select p;
                if (pchk.Count() == 0)
                {
                    wiadomosc.Visible = true;
                    calosc.Visible = false;
                    infop.Text = "Brak pacjenta o podanym id!";
                }
                else
                {
                    var query = from p in DE.PatientTeeth where p.idPatient == id select p;
                    try
                    {
                        if (Request.QueryString["z"].Equals("1"))
                        {
                            foreach (var tooth in query)
                            {
                                i++;
                                tooth.generalStatusId = (int)float.Parse(Request.QueryString["zab_" + i + "_G"]);
                                tooth.backSideStatusId = (int)float.Parse(Request.QueryString["zab_" + i + "_d"]);
                                tooth.leftSideStatusId = (int)float.Parse(Request.QueryString["zab_" + i + "_l"]);
                                tooth.frontSideStatusId = (int)float.Parse(Request.QueryString["zab_" + i + "_g"]);
                                tooth.rightSideStatusId = (int)float.Parse(Request.QueryString["zab_" + i + "_p"]);
                            }
                            VisitsHistory vh = new VisitsHistory();
                            vh.idEmployee = 1;
                            vh.idPatient = id;
                            vh.visitStatusId = 1;
                            vh.visitDate = DateTime.Now;
                            vh.visitDescription = Server.UrlDecode(Request.QueryString["o"]);
                            DE.AddToVisitsHistories(vh);
                            DE.SaveChanges();
                            i = 0;
                            Response.Redirect("Pacjent.aspx?p=" + id);
                        }
                    }
                    catch (Exception) { }
                    if (query.Count() == 0)
                    {
                        for (int j = 0; j < 32; j++)
                        {
                            PatientTooth pt = new PatientTooth();
                            pt.idPatient = id;
                            pt.idTooth = j + 1;
                            pt.generalStatusId = 7;
                            pt.backSideStatusId = 6;
                            pt.leftSideStatusId = 6;
                            pt.frontSideStatusId = 6;
                            pt.rightSideStatusId = 6;
                            DE.AddToPatientTeeth(pt);
                        }
                        DE.SaveChanges();
                    }
                    var query2 = from pa in DE.Patients where pa.id == id select pa;
                    var pacjent = query2.FirstOrDefault();
                    info.Text = "Imię i nazwisko: <b>" + pacjent.name + " " + pacjent.surname + "</b><br>Telefon: <b>" + pacjent.phone + "</b><br>E-mail: <b><a href=mailto:" + pacjent.email + ">" + pacjent.email + "</a></b><br>Adres: <b>" + pacjent.street + ", " + pacjent.cityCode + " " + pacjent.city + "</b>";
                    String kom = "d=1&p=" + id;
                    //foreach (var tooth in from p in DE.PatientTeeth where p.idPatient == id select p)
                    foreach (var tooth in query)
                    {
                        i++;
                        kom += "&zab_" + i + "_G=" + tooth.generalStatusId + "&zab_" + i + "_l=" + tooth.leftSideStatusId + "&zab_" + i + "_p=" + tooth.rightSideStatusId + "&zab_" + i + "_d=" + tooth.backSideStatusId + "&zab_" + i + "_g=" + tooth.frontSideStatusId;
                    }
                    dane.Text = kom;
                    var query3 = from vh in DE.VisitsHistories where vh.idPatient == id orderby (vh.id) descending select vh;
                    if (query3.Count() == 0)
                    {
                        historia.Text = "Brak informacji";
                    }
                    foreach (var visit in query3)
                    {
                        historia.Text += "<b>" + visit.visitDate + ":</b> " + visit.visitDescription + "<br />";
                    }
                }
            }
        }
    }
}